<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 用户列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <!-- 表格数据 start -->
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="account" label="账户"></el-table-column>
                <el-table-column prop="nick" label="昵称"></el-table-column>
                <el-table-column label="头像(查看大图)" align="center">
                    <template slot-scope="scope">
                        <el-image class="table-td-thumb" style="border-radius: 50%" :src="scope.row.avr"
                            :preview-src-list="[scope.row.avr]">
                        </el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="gender" label="性别"></el-table-column>
                <el-table-column prop="authPw" label="是否陪玩">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.authPw === 'y'" type="success">已认证</el-tag>
                        <el-tag v-else-if="scope.row.authPw === 'n'" type="danger">未认证</el-tag>
                        <el-tag v-else type="warning">待审核</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="birthday" label="生日"></el-table-column>
                <el-table-column prop="regTime" label="注册时间"></el-table-column>

                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.status === 'y'" type="success">正常</el-tag>
                        <el-tag v-else type="danger">锁定</el-tag>
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-lx-attention" @click="handleView(scope.$index, scope.row)">
                            查看 </el-button>
                        <el-button type="text" icon="el-icon-lx-roundcheck" class="green"
                            @click="handleAgree(scope.$index, scope.row)">通过</el-button>
                        <el-button type="text" icon="el-icon-lx-roundclose" class="red"
                            @click="handleRefuse(scope.$index, scope.row)">驳回</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 表格数据 end -->

            <!-- 分页区 start -->
            <div class="block">
                <el-pagination @current-change="handleCurrentChange" :current-page.sync="pageNum" :page-size="3"
                    layout="prev, pager, next, jumper" :page-count="totalPages">
                </el-pagination>
            </div>
            <!-- 分页区 end -->
        </div>

        <!-- 查看弹出框 -->
        <el-dialog title="陪玩申请" :visible.sync="viewVisible" width="40%">
            <el-form ref="form" :model="user" label-width="70px">
                <el-form-item label="用户信息" align="left">
                    <template slot-scope="scope">
                        <el-image class="user_pw_img" style="border-radius: 50%" :src="user.avr"
                            :preview-src-list="[user.avr]">
                        </el-image>
                        <span style="margin-right: 15px"><b>账户</b></span><span style="margin-right: 100px"> {{
                                user.account
                        }}</span>
                        <el-button type="text" icon="el-icon-lx-group" class="green" title="粉丝"
                            style="font-size: 20px; margin-right: 20px">
                            <span style="font-size: 10; color: silver">{{ user.fans }}</span>
                        </el-button>
                        <el-button type="text" icon="el-icon-lx-like" class="red" title="关注" style="font-size: 20px">
                            <span style="font-size: 10; color: silver">{{ user.likes }}</span>
                        </el-button><br />
                        <span style="margin-right: 15px"><b>昵称</b></span><span>{{ user.nick }}</span><br />
                        <span style="margin-right: 15px"><b>性别</b></span><span>{{ user.gender }}</span><br />
                        <span style="margin-right: 15px"><b>生日</b></span><span>{{ user.birthday }}</span><br />
                    </template>
                </el-form-item>

                <el-form-item label="提交图片">
                    <template v-for="userImg in user.userImages">
                        <el-image class="user_pw_img" :src="userImg" :preview-src-list="[userImg]"> </el-image>
                    </template>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="viewVisible = false">关闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'basetable',
    data() {
        return {
            userVo: {
                user: {
                    authPw: 'c'
                },
                pageNum: 1
            },
            user: {
                account: '',
                avr: '',
                nick: '',
                likesCount: 0,
                fansCount: 0,
                userImages: []
            },
            pageNum: 1,
            totalPages: 0,
            tableData: [],
            multipleSelection: [],
            delList: [],
            viewVisible: false,
            idx: -1,
            id: -1
        };
    },

    created() {
        this.getData();
    },

    methods: {
        // 获取 userList 数据
        getData() {
            this.$axios.post('/admin/user/list', this.userVo).then((r) => {
                this.tableData = r.data.data.data;
                console.log(r);
                this.totalPages = r.data.data.totalPages;
                console.log(this.totalPages);
            });
        },
        // 查看详情
        handleView(index, row) {
            this.user = row;
            this.viewVisible = true;
            this.$axios.get('/user/getUserImages', { params: { id: row.id } }).then((r) => {
                this.user.userImages = r.data.data;
            });
            this.$axios.get('/user/getFansCount/' + row.id).then((r) => {
                this.user.fans = r.data.data;
            });
            this.$axios.get('/user/getLikesCount/' + row.id).then((r) => {
                this.user.likes = r.data.data;
            });
        },
        // 通过审核
        handleAgree(index, row) {
            this.$axios.get('/admin/user/authPw/agree', { params: { id: row.id } }).then((r) => {
                if (r.data.code == 200) {
                    this.$message.success('通过成功');
                    this.tableData.splice(index, 1);
                } else {
                    this.$message.warning('操作失败，请稍候再试');
                }
            });
        },
        // 拒绝审核
        handleRefuse(index, row) {
            // 二次确认删除
            this.$confirm('确定要驳回用户  " ' + row.nick + ' "  的陪玩认证吗？', '提示', {
                type: 'warning'
            }).then(() => {
                this.$axios.get('/admin/user/authPw/refuse', { params: { id: row.id } }).then((r) => {
                    if (r.data.code == 200) {
                        this.$message.success('操作成功');
                        this.tableData.splice(index, 1);
                    } else {
                        this.$message.warning('操作失败，请稍候再试');
                    }
                });
            });
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },

        // 分页导航
        handleCurrentChange(pageNum) {
            this.userVo.pageNum = pageNum;
            this.getData();
            console.log(`当前页: ${pageNum}`);
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.green {
    color: #06710c;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

.user_pw_img {
    display: block;
    border: black 2px;
    margin-right: 20px;
    float: left;
    width: 150px;
    height: 150px;
}

.size-icon {
    font-size: 25px;
}
</style>
